import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {NativeServiceC} from '../../../providers/NativeServiceC';
import {HttpService} from '../../../providers/HttpService';
import {GlobalData} from "../../../providers/GlobalData";

import {ClassifyDetailPage} from "../classifyDetail/classifyDetail";

@Component({
    selector: 'page-classify',
    templateUrl: 'classify.html'
})
export class ClassifyPage {
    collections = [];
    Style = [];
    Class = [];
    constructor(public navCtrl:NavController,
                private nativeService:NativeServiceC,
                private globalData:GlobalData,
                private httpService:HttpService) {
    }

    ionViewDidLoad() {
        this.getCollections();
        this.getStyles();
    }


    private getCollections() {
        let url = '/v1/collections',
            sender = {
                limit: 6,
                offset: 0
            };
        this.httpService.get(url, sender).subscribe(res => {
            this.collections = res.collections;
            this.nativeService.hideLoading();
        });
    }

    private getStyles() {
        let url = '/v1/channel_groups/all';
        this.httpService.get(url).subscribe(res => {
            this.Style = res.channel_groups[0];
            this.Class = res.channel_groups[1];
            this.nativeService.hideLoading();
        })
    }

    private pushController(sender:Object){ // sender 必须有 title id 和 type(collections channels) 属性 为了简单实现 就不做model了
        this.navCtrl.push(ClassifyDetailPage, sender);
    }

    allClassify(collection){
        collection = collection || {};
        let sender = {
            title: collection['title'] ||'全部分类',
            id:collection['id'] || 0,
            type:'collections'
        };
        this.pushController(sender);
    }

    classifyClick(channel){
        let sender = {
            title: channel['name'],
            id:channel['id'],
            type:'channels'
        };
        this.pushController(sender);
    }
}

